/*
 * cs 2020-5-2
 * 处理 32000 转 3.2W 
 */
function numToString_w(num) {
	if (!num) {
		return "1K";
	}

	if (num >= 10000) {
		return (Math.floor(num / 100)) / 100 + "W"
	}
	if (num >= 1000 && num < 10000) {
		return (Math.floor(num / 10)) / 100 + "K"
	}
	if (num < 1000) {
		return "1K"
	}
}

/*
 * cs 2020-5-2
 * 处理图片检验
 */
function coverImgTest(imgSrc) {
	let reg = /(png|jpe?g|gif|svg)(\?.*)?$/;
	if (imgSrc && reg.test(imgSrc)) {
		return imgSrc;
	} else {
		return '../../static/img/common/coverDefault.jpg';
	}
}

/*
 * cs 2020-5-2
 * 时间格式化
 */
function dateFormat(date, format) {
	if (!date || !format) {
		return '';
	}
	if (typeof date != "number") {
		date = date + '';
		if (date.indexOf("T") < 0) {
			date = date.replace(/-/g, '/')
		}
	}
	date = new Date(new Date(date).getTime());

	var map = {
		M: date.getMonth() + 1,
		d: date.getDate(),
		h: parseInt(date.getHours()),
		m: date.getMinutes(),
		s: date.getSeconds(),
		q: Math.floor((date.getMonth() + 3) / 3),
		S: date.getMilliseconds() //毫秒
	};
	format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
		var v = map[t];
		if (v !== undefined) {
			if (all.length > 1) {
				v = '0' + v;
				v = v.substr(v.length - 2);
			}
			return v;
		} else if (t === 'y') {
			return (date.getFullYear() + '').substr(4 - all.length);
		}
		return all;
	});
	return format;
}

function NumToString(num) {
	if (!num) {
		return '00:00';
	}
	try {
		if (num >= 60 * 60) {
			var h = Math.floor((num / 60) * 60).toString();
			var m = Math.floor(num / 60).toString();
			var s = Math.round(num % 60).toString();
			h = h.length > 1 ? h : '0' + h;
			m = m.length > 1 ? m : '0' + m;
			s = s.length > 1 ? s : '0' + s;
			return m + ':' + s;
		} else {
			var m = Math.floor(num / 60).toString();
			var s = Math.round(num % 60).toString();
			m = m.length > 1 ? m : '0' + m;
			s = s.length > 1 ? s : '0' + s;
			return m + ':' + s;
		}
	} catch (err) {
		return '00:00';
	}
}


function his_dateFilter(time, isShowHours) {
	if (!time) {
		return ""
	}
	// 解决移动端时间 格式不正确的问题呢
	if (typeof time != "number") {
		time = time + '';
		if (time.indexOf("T") < 0) {
			time = time.replace(/-/g, '/')
		}
	}

	let date = new Date();
	let hisDate = new Date(time);
	let date_time = date.getTime();
	let hisDate_time = hisDate.getTime();

	let Y = date.getFullYear() == hisDate.getFullYear() ? '' : hisDate.getFullYear();
	// 今天开始时间
	let today = new Date(dateFormat(date, 'yyyy-MM-dd')).getTime() - 8 * 60 * 60 * 1000;
	// 今天结束时间
	let tomorrow = today + 24 * 60 * 60 * 1000;

	// 昨天开始时间
	let yesterday = today - 24 * 60 * 60 * 1000;

	let Md = '';
	if (hisDate_time >= today && hisDate_time < tomorrow) {
		Md = '今天';
	} else if (hisDate_time >= yesterday && hisDate_time < today) {
		Md = '昨天';
	} else {
		Md = dateFormat(hisDate, 'MM-dd');
	}

	let hm = dateFormat(hisDate, 'hh:mm');
	Y = Y.toString();
	Y = Y ? Y + "-" : ""
	Md = Md.toString();
	hm = hm.toString();
	if (isShowHours) {
		return Y + Md + " " + hm;
	} else {
		return Y + Md;
	}
}


function omitTheYear(time) {
	if (!time) {
		return ""
	}
	// console.log("-------")
	// 解决移动端时间 格式不正确的问题呢
	if (typeof time == "number") {

	} else {
		time = time.replace(/-/g, '/')
	}

	let date = new Date();
	let hisDate = new Date(time);

	let Md = dateFormat(hisDate, 'MM-dd');
	let Y = date.getFullYear() == hisDate.getFullYear() ? '' : hisDate.getFullYear();
	let hm = dateFormat(hisDate, 'hh:mm');
	Y = Y.toString();
	Y = Y ? Y + "-" : ""
	Md = Md.toString();
	hm = hm.toString();
	return Y + Md + ' ' + hm + '';
}

// 处理超过99为99+
function than99(num) {
	if (num > 99) {
		return "99+"
	} else {
		return num
	}
}

// 处理金额
function returnFloat(value) {
	if (!value) {
		return "0.00"
	}

	var value = Math.round(parseFloat(value) * 100) / 100;
	var xsd = value.toString().split(".");

	if (xsd.length == 1) {
		value = value + ".00";
		return addThousandthSign(value);
	}
	if (xsd.length > 1) {
		if (xsd[1].length < 2) {
			value = xsd[0].toString() + "." + xsd[1] + "0";
		}
		return addThousandthSign(value);
	}
};


function addThousandthSign(numStr) {
	var regForm = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g;
	return numStr.toString().replace(regForm, "$1,");
}

/**
 * @author chenshuai 2020-5-19
 * @function 获取字典的字段
 * @param {string} num 比较的值
 * @param {array} obj  取值数组
 * @param {string} key 取值属性
 * @param {string} code='code' 比较值的属性
 */
function getObjKey(num, obj, key, code = 'code') {
	let tampObj = obj.find(item => {
		return item[code] == num
	})
	if (tampObj) {
		return tampObj[key] || "未知"
	} else {
		return "未知"
	}
}

function getArrKeyNum(nowKey, arr, key) {
	let num = 0;
	arr.forEach(item => {
		if (key) {
			if (item[key] == nowKey) {
				num++;
			}
		} else {
			if (item == nowKey) {
				num++;
			}
		}
	})
	return num
}


// 分钟转小时
function minuteToHours(minute) {
	if (!minute) {
		return 0
	}

	if (minute <= 10) {
		return minute
	}

	let hours = Math.round(minute / 60 * 10) / 10;
	return hours
}

//通过生日 获取年龄
function getAge(birthday) {
	if (!birthday) {
		return ""
	}
	let date = new Date();
	let birthdayDate = new Date(birthday);
	return Math.floor((date.getTime() - birthdayDate.getTime()) / (365 * 24 * 60 * 60 * 1000));
}

export {
	numToString_w,
	coverImgTest,
	dateFormat,
	NumToString,
	his_dateFilter,
	than99,
	omitTheYear,
	returnFloat,
	getObjKey,
	getArrKeyNum,
	minuteToHours,
	getAge
}
